//
//  MoreResultViewController.m
//  CityFire
//
//  Created by  androidlongs on 2017/11/6.
//  Copyright © 2017年 刘养柱. All rights reserved.
//

#import "MoreResultViewController.h"
#import "MoreResultItemTableViewCell.h"
#import "UserInfoModel.h"

@interface MoreResultViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
//处理结果
@property (weak, nonatomic) IBOutlet UITextView *inputResultTextView;
//是否标记为误报
@property (weak, nonatomic) IBOutlet UIButton *selectButton;

@end

//提交批处理数据
@implementation MoreResultViewController
- (IBAction)commitMoreResultClick:(id)sender {
    
    
    
    if (!self.inputResultTextView.text.length) {
        [SVProgressHUD showInfoWithStatus:@"请输入处理结果 "];
        return;
    }
    
    NSString *commitNum = @"";
    NSString *tableName=@"";
    if (self.selectItemArray.count==1) {
        commitNum=[NSString stringWithFormat:@"%ld",((MonitorModel *)self.selectItemArray[0]).num];
        tableName=[NSString stringWithFormat:@"%@",((MonitorModel *)self.selectItemArray[0]).tableName];
    } else {
        commitNum=[NSString stringWithFormat:@"%ld",((MonitorModel *)self.selectItemArray[0]).num];
        tableName=[NSString stringWithFormat:@"%@",((MonitorModel *)self.selectItemArray[0]).tableName];
        for (NSInteger i=1; i<self.selectItemArray.count; i++) {
            MonitorModel *model = self.selectItemArray[i];
            commitNum = [NSString stringWithFormat:@"%@,%ld",commitNum,model.num];
            tableName = [NSString stringWithFormat:@"%@,%@",tableName,model.tableName];
        }
    }
    
    MonitorModel *model = self.selectItemArray[0];
    [self commitData:model :commitNum:tableName];
}
- (IBAction)selectButtonClick:(id)sender {
    if(self.selectButton.isSelected){
        [self.selectButton setSelected:NO];
    }else{
        [self.selectButton setSelected:YES];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    
    
    self.title=@"批处理";
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MoreResultItemTableViewCell" bundle:nil] forCellReuseIdentifier:@"MoreResultItemTableViewCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.separatorColor = [UIColor redColor];
    
}

-(void)commitData:(MonitorModel *) model:(NSString *) ops :(NSString *) tableName {
    
    UserInfoModel *userModel = [NSKeyedUnarchiver unarchiveObjectWithData:[NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_USERINFO]];
    NSInteger select =1;
    if([self.selectButton isSelected]){
        select=1;
    }else{
        select =0;
    }
    if (userModel) {
        
        NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
        //表名
        [dic setObject:tableName forKey:@"table_name"];
        //主键
        [dic setObject:ops forKey:@"num"];
        //处理意见
        [dic setObject:self.inputResultTextView.text forKey:@"record_usr_log"];
        //是否误报
        [dic setObject:[NSString stringWithFormat:@"%ld",select] forKey:@"misinformation"];
        [dic setObject:userModel.seeeionID forKey:@"sessionid"];
        [YQNetworking requestWithUrl:kWARNING_DEAL_URL requestType:YQHttpRequestTypePOST refreshRequest:YES cache:NO params:dic progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
            //        /api-w/warning/queryRealTimeWarning?
            //        username=admin&x_day=7&pageNo=1&pageSize=5
            
        } successBlock:^(id response, NSError *responseInfo) {
            
            NSError *error;
            
            if (responseInfo.code == 0) {
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_SESSIONID];
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_USERINFO];
                
                [SVProgressHUD showSuccessWithStatus:@"提交成功"];
                [self.navigationController popViewControllerAnimated:YES];
                
                
            }else {
                [SVProgressHUD showErrorWithStatus:@"服务异常"];
            }
            
        } failBlock:^(NSError *error) {
            [SVProgressHUD showErrorWithStatus:error.domain];
        }];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

//cell 的数量
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.selectItemArray.count;
}

// 分组的数量
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    return 204.0f;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MonitorModel * model = self.selectItemArray[indexPath.row];
    MoreResultItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MoreResultItemTableViewCell"];
    cell.model = model;
    
    return cell;
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
